package vn.co.thn.android.videohot.fragment;

import java.util.ArrayList;

import vn.co.thn.android.customview.ListViewCustom;
import vn.co.thn.android.customview.ListViewCustom.OnLoadMoreListener;
import vn.co.thn.android.customview.ListViewCustom.OnRefreshListener;
import vn.co.thn.android.serviceconnect.callback.RequestCallBack;
import vn.co.thn.android.serviceconnect.request.Param;
import vn.co.thn.android.serviceconnect.request.RequestService;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHApp;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.VHUrlApi;
import vn.co.thn.android.videohot.activity.VHMainActivity;
import vn.co.thn.android.videohot.adapter.VHVideoListAdapter;
import vn.co.thn.android.videohot.common.VHCommonFragment;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import vn.co.thn.android.videohot.util.VHUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class VHTimeLinePageFragment extends VHCommonFragment implements OnRefreshListener,OnLoadMoreListener{
	private String URL_API;
	private ListViewCustom lst;
	private VHVideoListAdapter adapter;
	private ArrayList<VHVideoItemData> mListData;
	private int mPage = 0;
	private String mDateStart, mDateEnd, mDate_new;

	public static VHTimeLinePageFragment newInstance(int timeline_code, String url_api) {
		VHTimeLinePageFragment fragment = new VHTimeLinePageFragment();
		Bundle data = new Bundle();
		data.putInt(VHConstants.TYPE_TIMELINE, timeline_code);
		data.putString(VHConstants.EXTRA_URL_API, url_api);
		fragment.setArguments(data);
		return fragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mListData = new ArrayList<VHVideoItemData>();
		adapter = new VHVideoListAdapter(getActivity(), mListData);
		URL_API = getArguments().getString(VHConstants.EXTRA_URL_API);
		mDateStart = VHApp.getPreference().getDateStart(VHUtil.getCurrentDateString());
		mDateEnd = VHApp.getPreference().getDateEnd(VHUtil.getCurrentDateString());
		if (URL_API.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			if (mDateEnd.equalsIgnoreCase(VHUtil.getCurrentDateString())) {
				if (mDateStart.equalsIgnoreCase(mDateEnd)) {
					VHApp.getPreference().setDateStart(VHUtil.getCurrentDateString());
					VHApp.getPreference().setDateEnd(VHUtil.getCurrentDateString());
					mDate_new = mDateStart;
				} else {
					mDate_new = mDateStart;
				}

			} else {
				VHApp.getPreference().setDateStart(mDateEnd);
				VHApp.getPreference().setDateEnd(VHUtil.getCurrentDateString());
				mDate_new = mDateEnd;
			}
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.vh_timeline_page_fragment, container, false);
		lst = (ListViewCustom) view.findViewById(R.id.lst);
		lst.setOnLoadMoreListener(this);
		lst.setOnRefreshListener(this);
		return view;
	}

	@Override
	public void onActivityCreated(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		lst.setRowLimit(18);
		lst.setAdapter(adapter);
		LoadData();
	}

	private void LoadData() {
		mPage = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		params.add(new Param("limit", VHConstants.LIMT_ROW));
		if (URL_API.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			params.add(new Param("date_new", mDate_new));
		}
		RequestService requestService = new RequestService(URL_API, VHVideoItemData.class, new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub
				mListData.clear();
				mListData.addAll(data);
				adapter.notifyDataSetChanged();
				lst.onLoadMoreComplete();
			}

		}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.setNetWorkCallBack((VHMainActivity) getActivity());
		requestService.Post();
	}

	@Override
	public void onDataRefresh() {
		// TODO Auto-generated method stub
		LoadData();
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		mPage = mPage+1;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		params.add(new Param("limit", VHConstants.LIMT_ROW));
		if (URL_API.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			params.add(new Param("date_new", mDate_new));
		}
		RequestService requestService = new RequestService(URL_API, VHVideoItemData.class, new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub
				if(data.size()==0){
					mPage = mPage-1;
				}
				mListData.addAll(data);
				adapter.notifyDataSetChanged();
				lst.onLoadMoreComplete();
			}

		}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.Post();
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		mPage = 0;
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("page", mPage));
		params.add(new Param("limit", VHConstants.LIMT_ROW));
		if (URL_API.equalsIgnoreCase(VHUrlApi.Api_Video_new)) {
			params.add(new Param("date_new", mDate_new));
		}
		RequestService requestService = new RequestService(URL_API, VHVideoItemData.class, new RequestCallBack<VHVideoItemData>() {

			@Override
			public void onResult(VHVideoItemData data, boolean isUpdate) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onResult(ArrayList<VHVideoItemData> data, boolean isUpdate) {
				// TODO Auto-generated method stub
				mListData.clear();
				mListData.addAll(data);
				adapter.notifyDataSetChanged();
				lst.onRefreshComplete();
			}

		}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.Post();
	}

}
